package com.five.system.controller;

import com.five.basic.result.PageList;
import com.five.dto.RoleDto;
import com.five.pojo.Role;
import com.five.result.Result;
import com.five.system.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/role")
@Slf4j
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("/queryPage")
    public Result queryPage(@RequestBody Role role) {
        try {
            PageList<Role> rolePageList = roleService.queryPage(role);
            return Result.success(rolePageList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }

    }
    @PostMapping("/queryAll")
    public Result queryAll() {
        try {
            List<Role> roleList = roleService.queryAll();
            return Result.success(roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    @PostMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        try {
            roleService.delete(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        try {
            roleService.deleteBatch(ids);
            return Result.success("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    @PostMapping("/save")
    public Result save(@RequestBody RoleDto roleDto) {
        log.info("保存菜单信息：{}", roleDto);
        try {
            if (roleDto.getId() != null) {
                roleService.updateRole(roleDto);
                return Result.success("更新成功");
            } else {
                roleService.addRole(roleDto);
                return Result.success("保存成功");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
